using System;
using System.Messaging;

namespace GoldStone.Common.Wrap {
    public class AutoCreatedMessageQueue {
        private MessageQueue msmq;

        public AutoCreatedMessageQueue(string queuePath) {
            if (!MessageQueue.Exists(queuePath)) {
                MessageQueue.Create(queuePath);
            }
            msmq = new MessageQueue(queuePath);
        }

        public static implicit operator MessageQueue(AutoCreatedMessageQueue mq) {
            return mq.msmq;
        }
    }

    public class AutoDeletedMessageQueue : IDisposable {
        private MessageQueue msmq;

        public AutoDeletedMessageQueue(string queuePath) {
            msmq = new AutoCreatedMessageQueue(queuePath);
        }

        public void Dispose() {
            MessageQueue.Delete(msmq.Path);
        }

        public static implicit operator MessageQueue(AutoDeletedMessageQueue mq) {
            return mq.msmq;
        }
    }
}